<script>
	import { updateVersion, setUserStorageSync, InitScene } from '@/utils/index.js'
	import { userLogin, bindUserId, userToken, getConfigInfo, userLog } from '@/network/api.js'
	export default {
		globalData:{
			activityForm: "",
			switchCustom: false,
			is_firstAdv: true // 小程序首次打开显示广告
		},
		onLaunch(options) {
			const _this = this
			uni.login({
				provider: "weixin",
				success(res){
					_this.globalData.code = res.code
					_this.handleLogin(res.code,options,1)
				}
			})
			/* v2.0开发版本 */
			setTimeout(() => {
				this.$isResolve()
			}, 3000);
		},
		onShow(options){
			updateVersion()
			if(options.query.inviterId){
				let userId = uni.getStorageSync("user_id")
				if(userId){
					this.handleBindUser(userId,options)
				}else{
					const _this = this
					uni.login({
						provider: "weixin",
						success(res){
							_this.globalData.code = res.code
							_this.handleLogin(res.code,options,2)
						}
					})
				}
			}
			if (options.scene == 1047 || options.scene == 1048 || options.scene == 1049) {
				options.query.inviterId = InitScene(options.query.scene).inviterId
			}
			getConfigInfo({key: 'register_tip'}).then(res=>{
				uni.setStorageSync("register_tip",res.data.value)
			})
			const params = {
				user_id: uni.getStorageSync("user_id"),
				is_first: 0,
				iv: options.query.inviterId || '',
				url: `${options.path}?id=${options.query.id}&inviterId=${options.query.inviterId}`
			}
			userLog(params)
		},
		methods:{
			async handleLogin(code,options,status){
				userLogin({code}).then(res=>{
					setUserStorageSync(res.data)
					if(options.query.inviterId){
						// uni.setStorageSync("inviterUrl",`${options.path}?id=${options.query.id}&inviterId=${options.query.inviterId}`)
						this.handleBindUser(res.data.userid,options)
					}
					let url = options.path
					if(options.query.inviterId){
						url += '?inviterId=' + options.query.inviterId
					}
					if(options.query.id){
						if(url.includes('?')){
							url += '&id=' + options.query.id
						}else{
							url += '?id=' + options.query.id
						}
					}
					const params = {
						user_id: res.data.userid,
						is_first: status == 1 ? 1 : 0,
						iv: options.query.inviterId || '',
						url: url
					}
					userLog(params)
				})
			},
			handleBindUser(id,options){
				let url = options.path
				if(options.query.inviterId){
					url += '?inviterId=' + options.query.inviterId
				}
				if(options.query.id){
					if(url.includes('?')){
						url += '&id=' + options.query.id
					}else{
						url += '?id=' + options.query.id
					}
				}
				const params = {
					userid: id,
					inviter: options.query.inviterId,
					url: url
				}
				bindUserId(params)
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uview-ui/index.scss";
	@import url("@/common/css/global.css");
	@import url("@/common/icon/iconfont.css");
	
	/*每个页面公共css */
	page {
		// background-image: linear-gradient(1deg, #F0F3F8 0%, #F0F3F8 15%, #F0F3F8 31%, #E5D8C7 60%, #D8BA8F 90%, #D8BA8F 100%);
		// background-image: linear-gradient(1deg, #F0F3F8 0%, #F0F3F8 15%, #F0F3F8 31%, #D8BA8F 95%, #D8BA8F 100%);
		background-repeat: no-repeat;
		background-color: #F0F3F8;
	}
	
	.ql-editor.ql-blank:before {
	  font-style: normal;
	  color: #6C6D70
	}
	
	image{
		display:block;
	}
</style>
